/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAL;


import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.*;
import java.util.Properties;
import javax.swing.JOptionPane;

/**
 *
 * @author Pham
 */
public final class ConnectionDB {
    
    
    Statement stmt;
    Connection conn;
    ResultSet rs;
    String username = "root", password = "",
            host = "localhost", dbname = "shopcomputer", dbms = "mysql";
    
    public ConnectionDB()
    {
//        conn = getConnectionDB();
        try{
            conn = getConnectionDB();
        }
        catch(SQLException ex)
        {
            System.err.printf(ex.getMessage());
        }
        
    }

    //create connection
    public Connection getConnectionDB() throws SQLException 
    {
        try{
            Properties prop = new Properties();
            prop.put("user", this.username);
            prop.put("password", this.password);
            conn = DriverManager.getConnection("jdbc:" + dbms + "://" + host + ":3306" + "/" + dbname + ""
                    + "?useUnicode=true&characterEncoding=UTF-8", prop);
            return conn;
        }
        catch(SQLException e)
        {
            JOptionPane.showMessageDialog(null, "Error: " + e.getMessage());
            return null;
        }
        finally{
            if(stmt != null)
                stmt.close();
        }
    }
    
    //close connection
    public void closeConn() throws SQLException{
        conn.close();
    }
    
    //method executeQuery
    public ResultSet executeQuery(String queryString) 
    {
        try
        {
            stmt = conn.createStatement();
            rs = stmt.executeQuery(queryString);
            return rs;
        }
        catch(SQLException e)
        {
            System.err.print("Error: " + e.getMessage());
            return null;
        }
        
    }
    
    //method executeUpdate
    public boolean exeUpdate(String queryString) 
    {
        try{
            stmt = conn.createStatement();
            return stmt.executeUpdate(queryString) == 1;
        }
        catch(SQLException ex)
        {
            System.err.print(ex.getMessage());
            return false;
        }
        
    }
    
    //
    public ResultSet preparedStmt(String sqlString) throws SQLException
    {
        PreparedStatement statement = conn.prepareStatement(sqlString);
        ResultSet rs = statement.executeQuery();
        return rs;
    }
    
    //md5
    public String getMD5(String input) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] messageDigest = md.digest(input.getBytes());
            BigInteger number = new BigInteger(1, messageDigest);
            String hashtext = number.toString(16);
            // Now we need to zero pad it if you actually want the full 32 chars.
            while (hashtext.length() < 32) {
                hashtext = "0" + hashtext;
            }
            return hashtext;
        }
        catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }
}
